package com.angel.web.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.SysMenu;
import com.angel.health.SysRole;
import com.angel.health.SysRoleExt;
import com.angel.provider.sys.SysManagerRoleProvider;
import com.angel.provider.sys.SysManagerUserRoleProvider;

/**
 * 高亚林
 * 2016年9月20日下午1:05:22
 */

@RequestMapping("/manag/role")
@Controller
public class SysManagerRoleController {

	@Autowired
	private SysManagerRoleProvider provider;
	@Autowired
	private SysManagerUserRoleProvider userroleProvider;
	
	//分页查询
	@RequestMapping("/index")
	public String index(SysRole role,Pager pagers,HttpServletRequest request){
		Pager pager = provider.findAll(role, pagers);
		request.setAttribute("pager", pager);
		return "manag/role";
	}
	
	//查询所有角色
	@RequestMapping("/findAllRole")
	@ResponseBody
	public String findAllRole(){
		List<SysRole> list = provider.findAllRole();
		return JsonUtil.toJson(list);
	}
	
	//批量删除
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String ids){
		int i = provider.delete(ids);
		return JsonUtil.toJson(i);
	}
	
	//删除单个角色
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(SysRole role){
		int i = provider.deleteById(role);
		return JsonUtil.toJson(i);
	}
	
	//保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(SysRole role,HttpServletRequest request){
		role.setCreateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.save(role);
		return JsonUtil.toJson(map);
	}
	
	//根据id获取数据
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(SysRole role){
		SysRoleExt ext = provider.findUserById(role);
		return JsonUtil.toJson(ext);
	}
	
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public String update(SysRole role,String menu,HttpServletRequest request){
		role.setUpdateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.update(role,menu);
		return JsonUtil.toJson(map);
	}
	
	//查询角色对应的所有用户
	@RequestMapping(value="showRoleUsers",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String showRoleUsers(Integer id,HttpServletRequest request){
		SysRole role = new SysRole();
		role.setId(id);
		SysRoleExt ext = provider.findUserById(role);
		List<Map<String, Object>> list = provider.showRoleUsers(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ext", ext);
		list.add(map);
		return JsonUtil.toJson(list);
	}
	
	//删除指定用户
	@RequestMapping("/deleteUserRole")
	public String deleteUserRole(Integer id){
		userroleProvider.delete(id);
		return "redirect:index";
	}
	
	//查询角色对应菜单
	@RequestMapping("/findMenuByRole")
	@ResponseBody
	public String findMenuByRole(Integer id){
		List<SysMenu> list = provider.findMenuByRole(id);
		return JsonUtil.toJson(list);
	}
}
